package com.hue.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hue.entity.EduCourse;
import com.hue.entity.R;
import com.hue.entity.vo.CourseInfo;
import com.hue.entity.vo.CoursePublishVo;
import com.hue.entity.vo.CourseQuery;
import com.hue.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 姚凤桥
 * @since 2021-06-13
 */
@RestController
@CrossOrigin
@RequestMapping("/course")
public class EduCourseController {
    @Autowired
    private EduCourseService eduCourseService;

    //添加课程
    @PostMapping("/add")
    public R add(@RequestBody CourseInfo courseInfo) {
        try {
            String id = eduCourseService.add(courseInfo);
            return R.ok().data("id", id);
        } catch (Exception e) {
            return R.error().data("msg", e.getMessage());
        }
    }

    //根据courseId查询一个course和description
    @GetMapping("/findById/{id}")
    public R findById(@PathVariable("id") String id) {
        try {
            CourseInfo courseInfo = eduCourseService.findById(id);
            return R.ok().data("courseInfo", courseInfo);
        } catch (Exception e) {
            return R.ok().data("msg", e.getMessage());
        }
    }

    //修改课程信息
    @PostMapping("/update")
    public R update(@RequestBody CourseInfo courseInfo) {
        try {
            eduCourseService.update(courseInfo);
            return R.ok().data("id", courseInfo.getId());
        } catch (Exception e) {
            return R.ok().data("msg", e.getMessage());
        }
    }

    @GetMapping("/findCoursePublishVoByCourseId/{courseId}")
    public R findCoursePublishVoByCourseId(@PathVariable("courseId") String courseId) {
        CoursePublishVo coursePublishVo = eduCourseService.findCoursePublishVoByCourseId(courseId);
        return R.ok().data("coursePublishVo", coursePublishVo);
    }

    //发布课程
    @GetMapping("/publish/{id}")
    public R publish(@PathVariable("id") String id) {
        return eduCourseService.publish(id) ? R.ok() : R.error();
    }

    //查询所有课程
    @GetMapping("/findAll")
    public R findAll() {
        List<EduCourse> courses = eduCourseService.list(null);
        return R.ok().data("courses", courses);
    }

    /*
        删除课程
     */
    @GetMapping("/deleteById/{id}")
    public R deleteById(@PathVariable("id") String id) {
        return eduCourseService.deleteById(id) ? R.ok() : R.error();
    }

    //条件查询课程
    @PostMapping("/conditionAndPageFind/{page}/{size}")
    public R conditionAndPageFind(@RequestBody CourseQuery courseQuery,
                                  @PathVariable("page") int page,
                                  @PathVariable("size") int size) {
        Page<EduCourse> eduCoursePage = new Page<>(page, size);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseQuery.getTitle())) {
            wrapper.like("title", courseQuery.getTitle());
        }
        if (!StringUtils.isEmpty(courseQuery.getStatus())) {
            wrapper.eq("status", courseQuery.getStatus());
        }
        eduCourseService.page(eduCoursePage, wrapper);
        return R.ok().data("eduCoursePage", eduCoursePage);
    }

    /*
        查询前8个课程，按照view_count降序
     */
    @GetMapping("findByViewCount")
    public R findByViewCount(){
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("view_count");
        wrapper.last(" limit 8");
        List<EduCourse> courses = eduCourseService.list(wrapper);
        return R.ok().data("courses",courses);
    }
}

